$(function () {
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var originalHref = $('.coupon-history').attr('href');
    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };
    if (currentHotel === undefined || currentHotel === '') {
        toastr.error(lang('toastr.grogshop'));
        return;
    }     
    var colorArray = ['background-green','background-yellow','background-blue','background-purple','background-red'];
    var requestCard = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Customer/hasUserVipCard', {
            "hotel": currentHotel
        }, 'post', function (data) {
            toggleLoading(false);
            var info = data.data;
            var cardHTML = '';
            if (info.length === 0) {
                cardHTML = $('#templateCardEmpty').clone().html();
            } else {
                $.each(info, function (key, value) {
                    var templatecardHTML = $($('#templateCardList').clone().html());
                    templatecardHTML.addClass(colorArray[key%5]);
                    templatecardHTML.attr({'data-current':value.hotelId,'data-vipid':value.vipInfoId,'data-color':key%5,'data-vipuserid':value.vipUserId});
                    templatecardHTML.find('.card-hotel-name').text(value.name);
                    templatecardHTML.find('.card-hotel-image').attr('data-original', value.logo);
                    cardHTML += templatecardHTML.prop('outerHTML');
                });
            }
            $('#card-list').html(cardHTML);
            $('#doc .card-hotel-image').lazyload({
                'skip_invisible': true
            });              
        }, function(error){
            toggleLoading(false); 
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        });        
    }
    requestCard();
    $('#doc').on('click', '.card-refresh', function (e) {
        e.preventDefault();
        requestCard();
    });
    $('#card-list').on('click','.card-detail',function(e){
       e.stopPropagation(); 
       var vipInfoId = $(this).data('vipid');
       var currentHotel = $(this).data('current');
       var vipUserId = $(this).data('vipuserid'); 
       var color = $(this).data('color');
       window.location.href = "/Home/MemberPage/cardinfo?hotel=" + currentHotel+'&no='+vipInfoId+'&cl='+color+ '&user=' + vipUserId;
    });
});
